package com.htfyun.fragment.read;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.htfyun.Constants;
import com.htfyun.utils.DebugLog;
import com.htfyun.utils.DisplayUtil;

import org.geometerplus.android.fbreader.FBReader;
import org.geometerplus.android.fbreader.covers.CoverManager;
import org.geometerplus.android.fbreader.util.AndroidImageSynchronizer;
import org.geometerplus.fbreader.Paths;
import org.geometerplus.fbreader.book.Book;
import org.geometerplus.fbreader.formats.PluginCollection;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.filetypes.FileType;
import org.geometerplus.zlibrary.core.filetypes.FileTypeCollection;
import org.geometerplus.zlibrary.ui.android.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sst on 2017/11/28.
 */

public class ReadAdapter extends RecyclerView.Adapter<ReadAdapter.MyViewHolder> {

    private LayoutInflater mInflater;
    private Context mContext;

    private List<Book> addedBookList = new ArrayList<>();

    private final AndroidImageSynchronizer ImageSynchronizer;
    private final CoverManager myCoverManager;


    public ReadAdapter(Activity context) {
        mInflater = LayoutInflater.from(context);
        mContext = context;

        ImageSynchronizer = new AndroidImageSynchronizer(context);

        final int coverHeight = DisplayUtil.dipToPix(context, Constants.COVER_HEIGHT);
        final int coverWidth = DisplayUtil.dipToPix(context, Constants.COVER_WIDTH);
        
        myCoverManager = new CoverManager(context, ImageSynchronizer, coverWidth /*15 / 32*/, coverHeight);

    }

    public void setItemsAndNotify(List<Book> items) {
        addedBookList.clear();
        addedBookList.addAll(items);
        notifyDataSetChanged();

    }

    public void setDatas(List<Book> items) {
        addedBookList.clear();
        addedBookList.addAll(items);

    }

    public void updateBook(Book book) {
        int index = -1;
        for (int i = 0; i < addedBookList.size(); i ++) {
            if (addedBookList.get(i).getPath().equals(book.getPath())) {
                index = i;
                break;
            }
        }
        if (index > 0) {
            addedBookList.remove(index);
            addedBookList.add(index, book);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.htfyun_read_adapter_item, parent, false);
        final MyViewHolder holder = new MyViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Book book = addedBookList.get(position);

                if (book != null) {
                    FBReader.openBookActivity(mContext, book, null);

                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        final Book book = addedBookList.get(position);

        holder.title.setText(book.getTitle());
        final ImageView coverView = holder.img;
        if (!myCoverManager.trySetCoverImage(coverView, book, PluginCollection.Instance(Paths.systemInfo(mContext)))) {

            final FileType fileType = FileTypeCollection.Instance.typeForFile(ZLFile.createFileByPath(book.getPath()));
            DebugLog.e("fileTyp.id = " + fileType.Id);
            coverView.setImageResource(R.drawable.ic_shelf_txt_with_rect_bg);
        }
    }

    @Override
    public int getItemCount() {
        return addedBookList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private ImageView img;

        public MyViewHolder(View view) {
            super(view);
            img = (ImageView) view.findViewById(R.id.imgBook);
            title = (TextView) view.findViewById(R.id.txtBookName);
        }
    }
}
